//
//  ST_MatchHistoryViewController.swift
//  btvideo
//
//  Created by mac on 2019/11/26.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyJSON
import NIMSDK

// 匹配历史
class ST_MatchHistoryViewController: UIViewController {
    
    static let itemWidth = ScreenWidth - (iPhoneXSeries ? 80 : 57*2)
    
    // MARK: - Properties (Private)
    fileprivate var scaling_carousel: ST_ScalingCarouselView!

    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.init(hexString: "#42CEDA")
        
        add_carousel()
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.right.equalToSuperview().offset(-16+22-11)
            make.top.equalToSuperview().offset(statusBarHeight+10-22+11)
        }
        
        view.addSubview(match_title_label)
        match_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight+90-20)
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(match_title_label.snp.bottom).offset(16)
        }
        
        
        
        view.addSubview(send_msg_btn)
        send_msg_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(scaling_carousel.snp.bottom).offset(58)
            make.width.equalTo(280)
            make.height.equalTo(40)
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
         if scaling_carousel != nil {
            scaling_carousel.deviceRotated()
         }
    }
//    MARK: - Properties
    
    var json_array = [JSON]()
    
    
    lazy var close_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("注册_icon_关闭")!)
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    lazy var match_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "你有\(json_array.count)个新好友！"
        control.textColor = Color.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 24, weight: UIFont.Weight.semibold)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "现在就去发消息吧，别错过这次机会哦！"
        control.textColor = Color.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        return control
    }()
    

    lazy var send_msg_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.white
        control.cornerRadius = 40/2.0
        control.setTitleForAllStates("发送消息")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#42CEDA")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(send_msg_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    // MARK: - Configuration
    
    private func add_carousel() {
        
        let frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        
        let carouselHeight = ST_MatchHistoryViewController.itemWidth+20+20
        let inSet:CGFloat = (ScreenWidth-ST_MatchHistoryViewController.itemWidth)/2.0
        scaling_carousel = ST_ScalingCarouselView(withFrame: frame, andInset: inSet)
        scaling_carousel.dataSource = self
        scaling_carousel.delegate = self
        scaling_carousel.showsHorizontalScrollIndicator = false
        scaling_carousel.backgroundColor = .clear
        
        scaling_carousel.register(cellWithClass: ST_MatchHistoryCell.self)
        
        view.addSubview(scaling_carousel)
        
        scaling_carousel.snp.makeConstraints { (make) in
            make.height.equalTo(carouselHeight)
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview().offset(iPhoneXSeries ? -20 : -5)
        }
    }
    
    
    //    MARK: - Event
    @objc func close_handle(){
        
        goback_previous_controller()
    }
    
    @objc func send_msg_handle(){
 
        
        if let indexPath = scaling_carousel.currentCenterCellIndex,let item = json_array[safe:indexPath.item]{
            
            
            if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self) {
                return
            }
            
            let vc = ST_PrivateChatViewController()
            
            vc.sessionID = item["accid"].stringValue
            
            vc.source = "匹配历史"
            
            BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: vc.sessionID, avatar: item["avatar"].stringValue, userName: item["nickname"].stringValue))
            
            vc.hidden_more_tool_btn = true
            
            GlobalPresent(toController: vc)
          
        }
        
    }
}

extension ST_MatchHistoryViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return json_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_MatchHistoryCell.self, for: indexPath)
    
//        DispatchQueue.main.async {
//            cell.setNeedsLayout()
//            cell.layoutIfNeeded()
//        }
        cell.bind_data(json: json_array[indexPath.item])
        
        return cell
    }
}

extension ST_MatchHistoryViewController: UICollectionViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //scaling_carousel.didScroll()
    }
}


//MARK: - Cell

class ST_MatchHistoryCell: ST_ScalingCarouselCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        mainView = UIView(frame: contentView.bounds)
        
        contentView.addSubview(mainView)
        mainView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        mainView.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(cover_image_view.snp.width)
        }
        
        
        
        mainView.addSubview(user_name_label)
        user_name_label.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
        }
        
        mainView.addSubview(user_name_left_image_view)
        user_name_left_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(user_name_label)
            make.width.equalTo(4)
            make.height.equalTo(6)
            make.right.equalTo(user_name_label.snp.left).offset(-4)
        }
        
//        mainView.addSubview(click_btn)
//        click_btn.snp.makeConstraints { (make) in
//            make.edges.equalToSuperview()
//        }
        
        mainView.cornerRadius = 15
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - Bind Data
    
    
    func bind_data(json:JSON){
        
        self.json = json
        
        
        cover_image_view.pin_setImage(from: json["avatar"].stringValue.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
//        user_name_label.text = json["age"].stringValue + "岁  " + json["nickname"].stringValue
        
        user_name_label.text = json["age"].stringValue + "岁  " + json["address"].stringValue
        
    }
    
//    MARK: - Properties
    fileprivate lazy var json = JSON.null
    
    fileprivate lazy var click_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(present_user_center), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = ST_MatchHistoryViewController.itemWidth/2.0
        control.borderColor = Color.white
        control.borderWidth = 3
        return control
    }()
    
    
    fileprivate lazy var user_name_label:UILabel = {
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()
    fileprivate lazy var user_name_left_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("注册_icon_三角标识")!)
        return control
    }()
    
    
//    MARK: - Handle
    @objc func present_user_center(){
       
        
        
    }
    
//    fileprivate lazy var gradient:ST_FWGradientView = {
//        let control = ST_FWGradientView()
//
//        control.bgLayer.colors = [UIColor.init(hexString: "#1F1F1F")!.cgColor,UIColor.init(hex: 0x060606, transparency: 0.4)!.cgColor,UIColor.init(hex: 0x000000, transparency: 0.4)!.cgColor]
//        control.bgLayer.locations = [0, 0.62, 1]
//        control.bgLayer.startPoint = CGPoint(x: 0.5, y: 0)
//        control.bgLayer.endPoint = CGPoint(x: 1, y: 1)
//        return control
//    }()
    
}
